/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller.SRV;

import entity.MDoctor;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import session.MDoctorFacade;

/**
 *
 * @author SUNTIPARB
 */
@WebServlet(name="SRV_MDoctorDetail",
                loadOnStartup = 1,
                urlPatterns={"/SRV/MDoctorDetail",
                "/SRV/MDoctorDetail/formMDoctor_Submit"})
public class SRV_MDoctorDetail extends HttpServlet {
   
    @EJB
    private MDoctorFacade mDoctorFacade;

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String userPath = request.getServletPath();
        HttpSession session = request.getSession();

        // if category page is requested
        if (userPath.equals("/SRV/MDoctorDetail")) {
            response.setContentType("text/html;charset=utf-8");
            String DoctorId = request.getParameter("id");
            if (!DoctorId.equals("-1")){
                List<MDoctor> lMDoctor;
                try {
                    //Query Java Object
                    lMDoctor = mDoctorFacade.findByDoctorId(DoctorId);
                    if (!lMDoctor.isEmpty()) {
                        request.setAttribute("DoctorId", DoctorId);
                        for (MDoctor obj : lMDoctor) {
                            request.setAttribute("txtName", obj.getName());
                            request.setAttribute("txtSurname", obj.getSurname());
                            if (obj.getSex()){
                                request.setAttribute("rdbSex_Male", "Checked");
                                request.setAttribute("rdbSex_Female", "");
                            }else{
                                request.setAttribute("rdbSex_Female", "Checked");
                                request.setAttribute("rdbSex_Male", "");
                            }
                            request.setAttribute("txtCode", obj.getCode());
                        }
                    }
                } catch (Exception ex) {
                    System.out.println("Error message" + ex.getMessage());
                } finally {
                    lMDoctor = null;
                }
            }else{
                request.setAttribute("DoctorId", DoctorId);
                request.setAttribute("txtName", "");
                request.setAttribute("txtSurname", "");
                request.setAttribute("rdbSex_Male", "Checked");
                request.setAttribute("rdbSex_Female", "");
                request.setAttribute("txtCode", "");
            }

            userPath = "/SRV/MDoctorDetail";
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as
        // 8-bit Unicode (e.g., for Czech characters)
        Boolean isDispatcher = true; //กำหนดว่า ต้องการให้เปลี่ยนหน้าหรือไม่ ? ต้องการ : ไม่ต้องการ
        String userPath = request.getServletPath();
        HttpSession session = request.getSession();


        if (userPath.equals("/SRV/MDoctorDetail/formMDoctor_Submit")) {

            String DoctorId = (request.getParameter("DoctorId") != null) ? request.getParameter("DoctorId") : "-1";
            String txtName = (request.getParameter("txtName") != null) ? request.getParameter("txtName") : "";
            String txtSurname = (request.getParameter("txtSurname") != null) ? request.getParameter("txtSurname") : "";
            String rdbSex = (request.getParameter("rdbSex") != null) ? request.getParameter("rdbSex") : "";
            String txtCode = (request.getParameter("txtCode") != null) ? request.getParameter("txtCode") : "";
            //Change rdbSex Value to Boolean
            if (rdbSex.equals("Male")){
                rdbSex = "true";
            }else{
                rdbSex = "false";
            }

            PrintWriter out = response.getWriter();
            try {
                //Query Java Object
                if (!DoctorId.equals("-1")){
                    mDoctorFacade.EditDoctor(DoctorId, txtName, txtSurname, rdbSex, txtCode);
                }else{
                    mDoctorFacade.AddDoctor(txtName, txtSurname, rdbSex, txtCode);
                }

            } catch (Exception ex) {
                System.out.println("Error message" + ex.getMessage());
            } finally {
                out.close();
            }

            isDispatcher = false;
        }

        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";

        try {
            if (isDispatcher) {
                request.getRequestDispatcher(url).forward(request, response);
            }
        } catch (Exception ex) {
            ex.printStackTrace();

        }

    }
}
